#include <iostream>
#include <string>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {
    }
    ListNode(int x) : val(x), next(nullptr) {
    }
    ListNode(int x, ListNode *next) : val(x), next(next) {
    }
};

class Solution {
public:
    ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
        ListNode *head = nullptr;
        ListNode *cur = nullptr;
        int c = 0;

        while (l1 || l2 || c > 0) {
            if (l1) {
                c += l1->val;
                l1 = l1->next;
            }
            if (l2) {
                c += l2->val;
                l2 = l2->next;
            }

            auto node = new ListNode(c % 10);
            if (!head) {
                head = node;
            } else {
                cur->next = node;
            }
            cur = node;
            c /= 10;
        }

        return head;
    }
};

#include <vector>

ListNode *createNode(const vector<int> &nums) {
    ListNode *head = nullptr;
    ListNode *cur = nullptr;
    for (int i : nums) {
        auto node = new ListNode(i);
        if (!head) {
            head = node;
        } else {
            cur->next = node;
        }
        cur = node;
    }
    return head;
}

void printNode(ListNode *node) {
    string str;
    while (node) {
        str = std::to_string(node->val) + str;
        node = node->next;
    }
    cout << str << endl;
}

int main(int argc, char *argv[]) {
    auto node1 = createNode({2, 4, 3});
    auto node2 = createNode({5, 6, 4});

    Solution sl;
    auto node3 = sl.addTwoNumbers(node1, node2);
    printNode(node3);

    return 0;
}